package org.hry.netty.delimiter;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.EmptyByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.Charset;

/**
 * @Desc
 * @Author houry
 * @Date 2021/3/26 21:26
 **/
public class DelimiterInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new DelimiterBasedFrameDecoder(1024,true, Unpooled.buffer().writeBytes("|".getBytes()), Unpooled.buffer().writeBytes("\n".getBytes())));
//        pipeline.addLast(new DelimiterBasedFrameDecoder(1024, new EmptyByteBuf());
        pipeline.addLast(new StringDecoder(Charset.forName("GBK")));
        pipeline.addLast(new StringEncoder(Charset.forName("GBK")));
        pipeline.addLast(new DelimiterHandler());
    }
}
